/**
 * Created by Administrator on 2014/12/19.
 */
var AddtoCart = (function(){
	var INIT = false;
	var animateCallbacks = [];
	var Movetocart = {
		endX : 0,
		endY : 0,
		ewidth : 0,
		eheight : 0,
		beginX : 0,
		beginY : 0,
		bwidth : 0,
		bheight : 0,
		addBtn : null,
		etarget : null,
		btarget : null,
		init : function(){
			this.etarget = $("#topCarticon span");
			this.btarget = $("#movetocartIcon");
			var f = this.etarget.offset();
			this.endX = f.left;
			this.endY = f.top;
			this.ewidth = this.etarget.innerWidth();
			this.eheight = this.etarget.innerHeight();
			this.bwidth = this.btarget.innerWidth();
			this.bheight = this.btarget.innerHeight();
		},
		animate : function(){
			var that = this;
			this.addBtn = $("#addCartBtn");
			var btnoff = this.addBtn.offset();
			this.beginX = btnoff.left+50;
			this.beginY = btnoff.top+0;
			this.btarget.css({"left":this.beginX,"top":this.beginY});
			this.btarget.hide().css("opacity",0);
			this.btarget.show().css({"opacity":1}).animate({
				"left" : that.endX,
				"top" : that.endY
			},{
				duration : 600,
				queue : true,
				complete : function(){
					that.btarget.css({
						"left":that.beginX,
						"top":that.beginY,
						"opacity":0
					}).hide();
					for(var i in animateCallbacks){
						animateCallbacks[i] && animateCallbacks[i]();
					}
				}
			})
		}
	};
	function _getData(){
		var tarTicket = $("#ticketList").children(".active");
		var lid = tarTicket.attr("data-lid");
		var tid = tarTicket.attr("data-tid");
		var aid = tarTicket.attr("data-aid") || "";
		var type = tarTicket.attr("data-type");
		var count = $("#countInp").val();
		return{
			lid : lid,
			tid : tid,
			aid : aid,
			p_type : type,
			count : count
		}
	}
	function add(callbacks){
		var url = G_CONFIG.url.addtocart;
		var data = _getData() || {};
		//console.log(data)
		callbacks = callbacks || {};
		var f = new Function();
		var callbacksuccess = callbacks.success || f;
		var callbackunLogin = callbacks.unLogin || f;
		var callbacktimeout = callbacks.timeout || f;
		var callbackerror = callbacks.error || f;
		var loading = callbacks.loading || f;
		var removeLoading = callbacks.removeLoading || f;
		loading();
		$.ajax({
			url : url,
			type : "POST",
			dataType : "JSON",
			timeout : G_CONFIG.ajaxtimeout,
			data : data,
			success : function(res){
				removeLoading();
				if(res && res.code == 200){
					callbacksuccess(res);
				}else if(res && res.code == 104){
					callbackunLogin(res);
				}else{
					callbackerror()
				}
			},
			error : function(xhr,status){
				removeLoading();
				if(status == "timeout"){
					callbacktimeout(xhr);
				}else{
					callbackerror(xhr)
				}
			}
		})
	}
	function animate(){
		if(!INIT){
			INIT = true;
			Movetocart.init();
		}
		Movetocart.animate();
	}
	return {
		add : add,
		animate : animate
	}

})();
var Detail = (function(){
	var origCount = 1;
	function init(){
		var buyWrap = $("#buyWrap");
		var addcartBtn = $("#addCartBtn");
		var buyBtn = $("#buyBtn");
		//库存
		buyWrap.on("click",".tbox",function(e){
			var target = $(e.target);
			var oprice = target.attr("data-oprice");
			var nprice = target.attr("data-nprice");
			var storage = target.attr("data-storage");
			selectTicket(target,oprice,nprice,storage);
			return false;
		}).on("click",".countGroup .btn",function(e){
			var target = $(e.target);
			var targetInp = target.siblings(".countInp");
			var count = targetInp.val();
			var storage = $("#storageCount").text();
			if(target.hasClass("plus")){
				addCount(targetInp,count,storage);
			}else{
				minuCount(targetInp,count,storage);
			}
			return false;
		}).on("click","#addCartBtn",function(e){ //添加到购物车
			var target = $(e.target);
			if(target.hasClass("loading")) return false;
			AddtoCart.animate();
			AddtoCart.add({
				loading : function(){
					target.val("请求中...").addClass("loading");
				},
				removeLoading : function(){
					target.val("加入购物车").removeClass("loading");
				},
				success : function(res){
					$("#topCarticon > .num").text(res.cartCount);
				},
				unLogin : function(res){
					G.Login.open();
				},
				timeout : function(){
					alert("加入购物车超时，请稍后重试..")
				},
				error : function(){
					alert("加入购物车失败，请稍后重试..")
				}
			});
		}).on("click","#buyBtn",function(e){ //预订
			var target = $(e.target);
			if(target.hasClass("loading")) return false;
			AddtoCart.add({
				loading : function(){
					target.val("预订中...").addClass("loading");
				},
				removeLoading : function(){
					target.val("预订").removeClass("loading");
				},
				success : function(res){
					window.location.href = G_CONFIG.url.jumpToCart_xiu;
				},
				unLogin : function(res){
					G.Login.open();
				},
				timeout : function(){
					alert("预订超时，请稍后重试..")
				},
				error : function(){
					alert("预订失败，请稍后重试..")
				}
			});
		}).on("focus","#countInp",function(e){
			origCount = $(e.target).val();
		}).on("change","#countInp",function(e){
			var inp = $(e.target);
			var ncount = inp.val();
			var ocount = origCount;
			var storage = $("#storageCount").text();
			changeCount(inp,ncount,ocount,storage);
		})
		$("#detailWrap").on("click",".tabHead .tabLi",function(e){
			var tarLi = $(e.target);
			var index = tarLi.index();
			if(index<0) return false;
			tarLi.addClass("active").siblings().removeClass("active");
			$("#detailWrap").children(".detailCon").hide().eq(index).show();
		})
		buyWrap.find(".tbox:first").trigger("click");
		$("#detailWrap").find(".tabHead .tabLi:first").trigger("click");
	}
	//选择不同票类，变化价格，原价，库存
	function selectTicket(target,oprice,nprice,storage){
		if(!nprice) return;
		target.addClass("active").siblings().removeClass("active");
		$("#countInp").val(1)
		$("#oprice").text(oprice);
		if(nprice){
			$("#nprice").text(nprice);
		}else{
			$("#opricew").hide();
		}
		if(storage == -1){
			$("#storagew").hide();
		}else{
			$("#storagew").show();
			$("#storageCount").text(storage)
		}
	}
	function addCount(targetInp,count,storage){
		count = Number(count);
		storage = Number(storage);
		var ncount = count+1;
		if(storage != -1 && ncount>storage){
			ncount = count;
			alert("购买数量不能超过库存量"+storage)
		}
		targetInp.val(ncount);

	}
	function minuCount(targetInp,count,storage){
		count = Number(count);
		var ncount = count-1;
		if(ncount<1){
			ncount = 1;
		}
		targetInp.val(ncount);
	}
	function changeCount(targetInp,ncount,ocount,storage){
		if(!G.isPositiveNum(ncount)){//如果不是正整数
			targetInp.val(ocount);
			return false;
		}
		if(storage && Number(ncount)>Number(storage)){
			targetInp.val(ocount);
			alert("购买数量不能超过库存量"+storage)
			return false;
		}
		ocount = ncount;
	}

	return{
		init : init
	}

})();












$(function(){
	Detail.init();
})